package com.eachonline.smart.web.comment;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eachonline.core.JsonResult;
import com.eachonline.core.Pagination;
import com.eachonline.smart.model.SmartComment;
import com.eachonline.smart.model.SmartCommentExample;
import com.eachonline.smart.model.SmartQuestion;
import com.eachonline.smart.model.SmartQuestionExample;
import com.eachonline.smart.model.SmartUser;
import com.eachonline.smart.service.QuestionService;
import com.eachonline.smart.service.SmartCommentService;
import com.eachonline.smart.service.SmartUserService;
import com.eachonline.smart.web.common.CommonUtil;
import com.eachonline.util.StringUtil;
import com.eachonline.util.UUIDUtil;

@Controller
@RequestMapping("/question")
public class QuestionController {
	
	@Autowired
	public SmartCommentService commentService;
	
	
	@Autowired
	public SmartUserService userService;
	
	@Autowired
	public QuestionService questionService;

	
	

	/**
	 * 
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/save",method = RequestMethod.POST)
	public JsonResult saveQuestion(HttpServletRequest request, SmartQuestion  question){
		
		SmartUser  user=CommonUtil.getCurrentUser(request);
		
		if(StringUtil.isEmpty(question.getQuestionTitle()))
		{
			return  new JsonResult(false, "问题标题不能为空");
		}

		if(StringUtil.isEmpty(question.getQuestionText()))
		{
			return  new JsonResult(false, "问题内容不能为空");
		}
		
		question.setCreateTime(new Date());
		question.setUserId(user.getSid());
		question.setSid(UUIDUtil.getUUID());
		question.setUserName(user.getUserName());
		questionService.insert(question);
		return  new JsonResult(true, "提问成功");
	}
	

	/**
	 * 
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete",method = RequestMethod.POST)
	public JsonResult deleteQuestion(HttpServletRequest request, String sid){
		
		questionService.deleteByPrimaryKey(sid);
		SmartCommentExample commentExa=new SmartCommentExample();
		commentExa.createCriteria().andBusinessIdEqualTo(sid);
		commentService.deleteByExample(commentExa);
		return  new JsonResult(true, "提问成功");
	}
	
	

	/**
	 * 
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/queryQuestion",method = RequestMethod.POST)
	public JsonResult saveQuestion(HttpServletRequest request,String classId){
		
		
		int start=1;
		if(StringUtil.isNotEmpty(request.getParameter("start")))
		{
		  start=Integer.valueOf(request.getParameter("start").toString());
		}
		
		int pageSize=8;
		if(StringUtil.isNotEmpty(request.getParameter("pageSize")))
		{
			pageSize=Integer.valueOf(request.getParameter("pageSize").toString());
		}
		int begin =pageSize*(start-1);
		
		SmartQuestionExample qexa =new SmartQuestionExample();
		qexa.createCriteria().andClassIdEqualTo(classId);
		Pagination<SmartQuestion>  pageInfo=questionService.queryByPage(qexa, begin, pageSize, " create_time desc");
		List<SmartQuestion> qesList=pageInfo.getDataList();
		if(!qesList.isEmpty())
		{
			for (SmartQuestion ques:qesList) {
				SmartUser user=userService.selectByPrimaryKey(ques.getUserId());
				ques.setUserHeadUrl(user.getUserHeadPicUrl());
				
				SmartCommentExample commentExa=new SmartCommentExample();
				commentExa.createCriteria().andClassIdEqualTo(classId).andBusinessIdEqualTo(ques.getSid());
				Pagination<SmartComment> commentInfo= commentService.queryByPage(commentExa, 0, 2, " create_time desc");
			    if(!commentInfo.getDataList().isEmpty())
			    {
			    	SmartComment comment=commentInfo.getDataList().get(0);
			    	SmartUser cuser=userService.selectByPrimaryKey(comment.getUserId());
			    	comment.setUserHeadUrl(cuser.getUserHeadPicUrl());
			    	ques.setComment(comment);
			    }
			}
			
			
		}
		pageInfo.setDataList(qesList);
		 return new JsonResult(true, pageInfo);
	}
	
}
